<?php
/**
 * Created by PhpStorm.
 * User: lelouchms
 * Date: 2019/3/19
 * Time: 14:06
 */

namespace app\common\util;

class CommonUtil
{

    const WEI = 6;


    /**
     * 加密密码
     * @param string $password 原密码
     * @return array
     */
    public static function encodePassword($password, $salt)
    {
        return md5(md5($password) . $salt);
    }

    public static function add($num1, $num2)
    {
        return bcadd($num1, $num2, self::WEI);
    }

    public static function sub($num1, $num2)
    {
        return bcsub($num1, $num2, self::WEI);
    }

    public static function mul($num1, $num2)
    {
        return bcmul($num1, $num2, self::WEI);
    }

    public static function div($num1, $num2)
    {
        return bcdiv($num1, $num2, self::WEI);
    }

    public static function joinString($valueArr, $symbol)
    {
        if (empty($valueArr)) return "";
        $joinData = $symbol . join($symbol, $valueArr) . $symbol;
        return $joinData;
    }

    public static function splitArr($valueString, $symbol)
    {
        if (empty($valueString)) return [];
        $splitData = [];
        foreach (explode($symbol, $valueString) as $value) {
            if (!empty($value) && $value != $symbol) {
                if(is_nan($value)){
                    $splitData[] = $value;
                }else{
                    $splitData[] = intval($value);
                }
            }
        }
        return $splitData;
    }

    public static function isCurrency($value)
    {
        $flag = preg_match("/^\\d+(\\.\\d{1,2})?$/", $value, $match);
        if (empty($flag)) return false;
        return true;
    }

    public static function formatHostUrl($url)
    {
        return $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $url;
    }

}